package com.example.biye;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.content.pm.PackageManager;

import com.example.biye.database.UserDBHelper;
import com.example.biye.util.PermissionUtil;
import com.example.biye.bean.UserInfo;
import com.example.biye.util.DateUtil;
import com.example.biye.database.UserDBHelper;


public class MainActivity extends AppCompatActivity {
    private EditText et_account;
    private String maccont = "18403009754";
    private EditText et_password;
    private String mpassword = "111111";
    private CheckBox checkBox;
    private SharedPreferences mShared;
    private boolean bRemember = false;
    private SharedPreferences.Editor editor;
    private UserDBHelper mHelper;

    private class CheckListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView.getId() == R.id.checkBox) {
                bRemember = isChecked;
                if (isChecked) {
                    mShared.edit().putBoolean("ISCHECK", true).commit();
                } else {
                    mShared.edit().putBoolean("ISCHECK", false).commit();
                }
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        Button buttond = (Button) findViewById(R.id.buttond);
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        checkBox = (CheckBox)findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(new CheckListener());
        mShared = getSharedPreferences("share_login", MODE_PRIVATE);
        Log.i("mShared", String.valueOf(mShared.hashCode()));
        String account = mShared.getString("account", "");
        String password = mShared.getString("password", "");
        et_account.setText(account);
        et_password.setText(password);
        bRemember = mShared.getBoolean("ISCHECK", false);
        Log.i("bRemember", String.valueOf(bRemember));
        checkBox.setChecked(bRemember);

        buttond.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String account = et_account.getText().toString();
                        String password = et_password.getText().toString();
                        if (account.length() < 11) {
                            Toast.makeText(MainActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (!et_account.getText().toString().equals(maccont)) {
                            Toast.makeText(MainActivity.this, "该账号不存在，请及时注册", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (!et_password.getText().toString().equals(mpassword)) {
                            Toast.makeText(MainActivity.this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
                        } else {
                            if (bRemember) {
                                SharedPreferences.Editor editor = mShared.edit();
                                editor.putString("account", et_account.getText().toString());
                                editor.putString("password", et_password.getText().toString());
                                editor.commit();//存储数据
                            } else {
                                SharedPreferences.Editor editor = mShared.edit();
                                editor.putString("account", "");
                                editor.putString("password", "");
                                editor.commit();
                            }
                            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                            startActivity(intent);
                        }
                    }
                }

        );
    }
}